//
//  VisitorViewController.swift
//  Mircoblog-swift
//
//  Created by zz on 16/2/24.
//  Copyright © 2016年 zz. All rights reserved.
//

import UIKit
import SnapKit

protocol VisitorViewDelegate: NSObjectProtocol{
    
    /// 访客视图注册
    func visitorViewDidRegister()
    /// 访客视图登录
    func visitorViewDidLogin()
    
}
class VisitorView: UIView {

    // MARK: - 代理属性
    /// 提示：代理一定不要忘记 weak
    weak var delegate: VisitorViewDelegate?
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUpUI()
        
        //添加点击事件
        registerButton.addTarget(self, action: "register", forControlEvents: .TouchUpInside)
        loginButton.addTarget(self, action: "login", forControlEvents: .TouchUpInside)
        
//
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    //MARK: - public method
    /// 访客界面设置
    func setupInfo(imageName: String? = nil, message: String){
       
        messageLabel.text = message
        
        guard let imageName = imageName else {
        
            animation()
            return
        }
        //
        maskerView.hidden = true
        homeView.hidden = true
        iconView.image = UIImage(named: imageName)
    }
    //MARK: - 点击事件
    @objc private func register(){
        
        self.delegate?.visitorViewDidRegister()
    }
    @objc private func login(){
        
        self.delegate?.visitorViewDidLogin()
    }
    // MARK: - 私有控件
    /// 图标视图
    private lazy var iconView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    /// 遮盖层
    private lazy var maskerView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    /// 小房子图标
    private lazy var homeView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    /// 消息标签
    private lazy var messageLabel: UILabel = {
    
        let label = UILabel()
        label.text = "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
        label.textColor = UIColor.darkGrayColor()
        label.numberOfLines = 0
        label.font = UIFont.systemFontOfSize(14)
        
        return label
    }()
    /// 注册按钮
    private lazy var registerButton: UIButton = {
        let button = UIButton()
        
        button.setTitle("注册", forState: .Normal)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        button.setTitleColor(UIColor.orangeColor(), forState: .Normal)
//        button.sizeToFit()当设置大小的时候，不起作用
        //view不能
//        button.addTarget(self, action: "registor", forControlEvents: .TouchUpInside)
        return button
    }()
    
    /// 登录按钮
    private lazy var loginButton: UIButton = {
        let button = UIButton()
        
        button.setTitle("登录", forState: .Normal)
        button.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
          button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)

        return button
    }()



}

// MARK: - 设置动画
extension VisitorView{

    private func animation(){
    
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        
        anim.toValue = 2 * M_PI
        anim.repeatCount = MAXFLOAT
        anim.duration = 20
        // 提示：对于需要循环播放的动画，可以设置此属性为 false
        // 视图离开屏幕后，不会被销毁！当 动画所在视图被销毁后，动画会连带销毁
        // 不需要考虑释放问题
        anim.removedOnCompletion = false
        
        iconView.layer.addAnimation(anim, forKey: nil)
}
}
// MARK: - 设置界面
extension VisitorView{

    private func setUpUI(){
    
        // 1. 添加控件
        addSubview(iconView)
        addSubview(maskerView)
        addSubview(homeView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)

        // 2. 自动布局
        // 1> 参照属性一致，可以省略
        // 2> 参照属性不一致，使用 snp_参照属性 的格式
        iconView.snp_makeConstraints { (make) in
            
            make.centerX.equalTo(self)
            make.centerY.equalTo(self).offset(-60)
            
        }
        homeView.snp_makeConstraints { (make) in
            
//            make.centerY.equalTo(iconView)
//            make.centerX.equalTo(iconView)
            //上面两句代码可以写成一句，但有时候会出问题（size、center、edges）
            make.center.equalTo(iconView)
     
        }
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconView)
            make.top.equalTo(iconView.snp_bottom).offset(20)
            make.size.equalTo(CGSize(width: 224, height: 36))
        }
        registerButton.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(messageLabel.snp_bottom).offset(20)
            make.left.equalTo(messageLabel)
            make.size.equalTo(CGSize(width: 100, height: 36))
        }
        loginButton.snp_makeConstraints { (make) -> Void in
            make.right.equalTo(messageLabel)
            make.top.equalTo(registerButton)
            make.size.equalTo(registerButton)
        }
        maskerView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(self)
            make.right.equalTo(self)
            make.top.equalTo(self)
            make.bottom.equalTo(registerButton)
        }
        // 空白区域可以使用背景色(能够使用颜色，就不要使用图片)
        backgroundColor = UIColor(white: 237.0 / 255.0, alpha: 1.0)
    }
}


